package edu.uni.dormitory.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import edu.uni.bean.Result;
import edu.uni.bean.ResultType;
import edu.uni.dormitory.bean.LiveBed;
import edu.uni.dormitory.bean.StudentBed;
import edu.uni.dormitory.bean.StudentBedExample;
import edu.uni.dormitory.globalbean.ClassVo;
import edu.uni.dormitory.globalbean.StudentBedClassVo;
import edu.uni.dormitory.globalbean.StudentBedVo;
import edu.uni.dormitory.mapper.StudentBedMapper;
import edu.uni.dormitory.service.BedService;
import edu.uni.dormitory.service.StudentBedService;
import edu.uni.example.config.ExampleConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author 陈楚城
 * @create 2019.05.03
 * @function 服务
 */
@Service
public class StudentBedServiceImpl implements StudentBedService {
    @Autowired
    private StudentBedMapper studentBedMapper;
    @Autowired
    private ExampleConfig globalConfig;
    @Autowired
    private BedService bedService;


    @Override
    public boolean insert(StudentBed studentBed) {
        return studentBedMapper.insert(studentBed)>0 ? true : false;
    }

    @Override
    public boolean delete(long id) {
        return studentBedMapper.deleteByPrimaryKey(id)>0 ? true : false;
    }

    /**
     * 根据id查找
     * @param id
     * @return
     */
    @Override
    public StudentBed selectByPrimaryKey(Long id) {
        return studentBedMapper.selectByPrimaryKey(id);
    }

    /**
     * 调换宿舍后修改学生住宿信息
     * @param studentBedVo
     * @param bywho
     * @return
     */
    @Override
    public boolean updatestudentBed(StudentBedVo studentBedVo, long bywho) {
        if (studentBedVo != null && studentBedVo.getId() != null) {
            //根据学生的性别类型获取空床位
            Byte sex = studentBedVo.getUserSex().byteValue();
            Byte status1 = 1;//无人住
            Byte status0 = 0;//有人住
            List<LiveBed> listBedNO = bedService.selectBedNO(sex, status1);
            LiveBed liveBed = listBedNO.get(0);//获取一个空的床位
            long liveBedId = liveBed.getId();//获取这个新床位的id
            //修改这个床位为有人住
            liveBed.setStatus(status0);
            boolean success = bedService.update(liveBed);
            if (success) {
                //设置原来床位为空床位
                LiveBed liveBed1 = bedService.selectByPrimaryKey(studentBedVo.getLiveBedId());
                liveBed1.setStatus(status1);
                boolean success1 = bedService.update(liveBed1);
                if (success1) {
                    //更新原来的学生住宿信息
                    StudentBed studentBed=studentBedMapper.selectByPrimaryKey(studentBedVo.getId());
                    studentBed.setLeaveDate(new Date());
                    long deleted1 = 1;//无效
                    long deleted0 = 0;//有效
                    studentBed.setDeleted(deleted1);
                    boolean success2 = studentBedMapper.updateByPrimaryKeySelective(studentBed)>0 ? true:false;
                    if (success2) {
                        //新增修改的信息
                        StudentBed studentBed1 = new StudentBed();
                        studentBed1.setStudentId(studentBedVo.getStudentId());
                        studentBed1.setLiveBedId(liveBedId);
                        studentBed1.setLiveDate(new Date());
                        studentBed1.setLeaveDate(null);
                        studentBed1.setStatus(status0);
                        studentBed1.setDatetime(new Date());
                        studentBed1.setByWho(bywho);//这个需要登录人的id
                        studentBed1.setDeleted(deleted0);
                        boolean success3 = studentBedMapper.insert(studentBed1)>0 ? true :false;
                        if (success3) {
                             /* cache.delete(CacheNameHelper.Receive_CacheNamePrefix + studentBed.getId());
                            cache.deleteByPaterm(CacheNameHelper.List_A_CacheNamePrefix + "*");
                            cache.deleteByPaterm(CacheNameHelper.List_E_CacheNamePrefix + "*");
                            cache.deleteByPaterm(CacheNameHelper.ListByAid_CacheNamePrefix + "*");
                            cache.deleteByPaterm(CacheNameHelper.ListByEid_CacheNamePrefix + "*");*/
                            return true;
                        } else {
                            return false;
                        }
                    }
                }

            }

        }
        return false;
    }

    @Override
    public boolean update(StudentBed studentBed) {
        return studentBedMapper.updateByPrimaryKeySelective(studentBed)>0 ? true :false;
    }

    /**
     * 根据学生id查学生住宿信息
     * @param studentId
     * @return
     */
    @Override
    public StudentBedVo selectStudentBedBySid(long studentId) {
        return studentBedMapper.selectStudentBedBySid(studentId);
    }


    /**
     * 根据宿舍区id获取学生宿舍信息
     * @param areaId
     * @return
     */
    @Override
    public List<StudentBedVo> selectStudentBedByAid(long areaId) {
        //多表关联查询
        return studentBedMapper.selectStudentBedByAid(areaId);
    }

    /**
     * 根据辅导员id查询学生住宿信息
     */
    @Override
    public List<StudentBedClassVo> selectCid(long classId) {
        //多表联合查询
        return studentBedMapper.selectStudentBedByCid(classId);
    }

    /**
     * 分页查询宿舍区学生住宿信息
     * @param pageNum
     * @param areaId
     * @return
     */
    @Override
    public PageInfo<StudentBedVo> selectPageByAreaId(Integer pageNum, long areaId) {
        PageHelper.startPage(pageNum, globalConfig.getPageSize());    // 开启分页查询，第一次切仅第一次查询时生效
        List<StudentBedVo> list=studentBedMapper.selectStudentBedByAid(areaId);
        if(list != null)
            return new PageInfo<>(list);
        else
            return null;

    }
    //查询班级多少人
    @Override
    public int selectclasspersonNum(long classId) {
        return studentBedMapper.selectclasspersonNum(classId);
    }

    /**
     * 分页查询学院学生住宿信息
     * @param pageNum
     * @param employeeId
     * @return
     */
    @Override
    public PageInfo<StudentBedClassVo> selectPageByEmployeeId(Integer pageNum, long employeeId) {
        PageHelper.startPage(pageNum, globalConfig.getPageSize());    // 开启分页查询，第一次切仅第一次查询时生效
        List<StudentBedClassVo> list=studentBedMapper.selectStudentBedByCid(employeeId);
        if(list != null)
            return new PageInfo<>(list);
        else
            return null;
    }

    /**
     * 宿管中心查询所有宿舍区的学生住宿信息
     * @return
     */
    @Override
    public List<StudentBedVo> selectStudentBedAll() {
        return studentBedMapper.selectStudentBedAll();
    }

    /**
     * 分页宿管中心查询所有宿舍区的学生住宿信息
     * @param pageNum
     * @return
     */
    @Override
    public PageInfo<StudentBedVo> selectStudentBedAllByPage(Integer pageNum) {
        PageHelper.startPage(pageNum, globalConfig.getPageSize());
        List<StudentBedVo> list=studentBedMapper.selectStudentBedAll();
        if(list != null)
            return new PageInfo<>(list);
        else
            return null;
    }
    //根据辅导员id查询这个学院的班级信息

    @Override
    public List<ClassVo> selectclassbyEid(long employeeId) {
        return studentBedMapper.selectclassbyEid(employeeId);
    }

    @Override
    public List<StudentBedClassVo> selective(StudentBedClassVo studentBedClassVo) {
        return studentBedMapper.selective(studentBedClassVo);
    }
}
